﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Controls;
using JXPlatform.GIS.ViewModels;

namespace JXPlatform.GIS.Views
{
    /// <summary>
    /// Interaction logic for MapView.xaml
    /// </summary>
    [Export]
    public partial class MapView
    {
        private AxMapControl _mapControl;
        private AxToolbarControl _toolbarControl;
        private bool _isInitializedMapControls;

        public MapView()
        {
            InitializeComponent();
            CreateMapControl();

            Loaded += OnLoaded;
        }

        private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
        {
            InitializeMapControls();
        }

        private void InitializeMapControls()
        {
            if (_isInitializedMapControls)
            {
                return;
            }

            _toolbarControl.SetBuddyControl(_mapControl);

            //add command and tools to the toolbar
            _toolbarControl.AddItem("esriControls.ControlsMapNavigationToolbar");
            _toolbarControl.AddItem("esriControls.ControlsMapIdentifyTool");

            _isInitializedMapControls = true;
        }

        private void CreateMapControl()
        {
            _mapControl = new AxMapControl();
            mapHost.Child = _mapControl;

            _toolbarControl = new AxToolbarControl();
            toolbarHost.Child = _toolbarControl;
        }

        [Import]
        public MapViewModel MapViewModel
        {
            get { return DataContext as MapViewModel; }
            set { DataContext = value; }
        }

        public AxMapControl MapControl
        {
            get { return _mapControl; }
        }

        public AxToolbarControl ToolbarControl
        {
            get { return _toolbarControl; }
        }
    }
}
